﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace SampleApplication.Data.ValueConverters
{
    /// <summary>
    /// IntegerOffsetConverter will offset an integer value by the amount specified in the 
    /// offset property. This is a one way conversion
    /// </summary>
    public sealed class IntegerOffsetConverter : IValueConverter
    {
        /// <summary>
        /// The positive or negative amount to offset the value by
        /// </summary>
        public int Offset { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //We'll accept integers as well as strings that can be converted to integers
            var intValue = value as int?;
            if (intValue == null)
            {
                double outVal;
                if (!Double.TryParse((value ?? 0).ToString(), out outVal)) { value = 0; }

                //If the value came in as something other than an int, let's say a string.
                //Then we want to give it back as a string.
                return System.Convert.ChangeType(System.Convert.ToInt32(value) + Offset, value.GetType());
            }
            else
            {
                return intValue.Value + Offset;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
